package com.webshop.DataBase;

import com.webshop.JavaBean.Address;
import com.webshop.service.AddressService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/AddressServlet")
public class AddressServlet extends HttpServlet {
    AddressService as = new AddressService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String method = request.getParameter("method");
        switch (method) {
            case "insert":
                addAdd(request, response);
                break;
            case "delete":
                deleteInfo(request, response);
                break;
            case "show":
                showInfo(request, response);
                break;
            case "modify":
                modifyInfo(request, response);
                break;
            case "select":
                selectAddress(request,response);
                break;
            case "find":
                find(request,response);
                break;
                default:
                    break;
        }
    }

    /**
     * 修改用户地址信息
     * 需要前台传上来 ID 的值。
     * @param request
     * @param response
     */
    private void modifyInfo(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String user = String.valueOf(session.getAttribute("name"));
/*        String user = String.valueOf(session.getAttribute("name"));*/
        String ID = request.getParameter("ID");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        String addressee = request.getParameter("addressee");
        Address temp = new Address(ID,user, address, phone, addressee);
        boolean flag = as.modifyAddressInfo(temp);
        if (flag) {
            showInfo(request, response);
        }else {
            try {
                response.sendRedirect("/test.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 显式用户所有地址信息。
     * @param request
     * @param response
     */
    private void showInfo(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("name");
        String name = String.valueOf(obj);
        List<Address> userAddress = as.getUserAddress(name);
        System.out.println("啦啦");

        //获取了用户信息，接下来返回给前端。
        request.setAttribute("userAddress", userAddress);
        try {
                                            //把这里的地址改了。
            request.getRequestDispatcher("/html/geren1.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除用户地址信息
     * 需要前台传上来 ID 的值。
     * @param request
     * @param response
     */
    private void deleteInfo(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("ID");
        boolean flag = as.deleteAddressInfo(id);
        //删除完成后，转到显式所有地址的界面。
        if (flag) {
            showInfo(request, response);
        }else {
            try {
                response.sendRedirect("/test.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 新增用户地址信息
     * @param request
     * @param response
     */
    private void addAdd(HttpServletRequest request, HttpServletResponse response) {

        HttpSession session = request.getSession();
        String user = String.valueOf(session.getAttribute("name"));
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        String addressee = request.getParameter("addressee");
        Address temp = new Address(user, address, phone, addressee);
        boolean flag = as.addAddress(temp);
        if (flag) {
            showInfo(request, response);
        }else {
            try {
                response.sendRedirect("/test.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取用户指定的地址，下单时使用。
     * @param request
     * @param response
     * @return
     */
    private void selectAddress(HttpServletRequest request, HttpServletResponse response)
    {
        String ID = request.getParameter("ID");
        Address address = as.selectAddress(ID);
        request. setAttribute("ChoiceAddress", address);
        try {

            request.getRequestDispatcher("path").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String S=req.getParameter("ID");
        Address address=new Address();
        address=as.selectAddress(S);
       /* req.setAttribute("hello",address.getAddress());*/
        System.out.println(address.getAddress());
        req.setAttribute("address",address.getAddress());
        req.setAttribute("addressee",address.getAddressee());
        req.setAttribute("phone",address.getPhone());
        req.setAttribute("ID",address.getID());
        System.out.println(address.getAddress());
        req.getRequestDispatcher("/html/geren2.jsp").forward(req,resp);
        System.out.println(address.getAddress());
    }
}
